package com.dtwave.wechat.service.provider.web.sys;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.dtwave.dubbo.DubboResult;
import com.dtwave.wechat.service.api.dto.PageResultDto;
import com.dtwave.wechat.service.api.dto.req.ReqPageQueryDto;
import com.dtwave.wechat.service.api.dto.req.ReqUserDto;
import com.dtwave.wechat.service.provider.database.entity.UserEntity;
import com.dtwave.wechat.service.provider.service.UserService;
import com.dtwave.wechat.service.provider.web.BaseController;
import com.dtwave.wolf.utils.BizPreconditions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: liuwenxiang
 * @Date: 2018/12/17 19:18
 * @Description:
 */

@Api(description = "用户接口")
@RestController
@RequestMapping("${url.common.path}/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;


    @ApiOperation(value="获取用户列表",notes="")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public DubboResult list(@Valid ReqUserDto req) {

        req.setSort(StringUtils.isNotBlank(req.getSort()) ? req.getSort() : ReqPageQueryDto.DB_MTIME);
        req.setOrder(StringUtils.isNotBlank(req.getOrder()) ? req.getOrder() : ReqPageQueryDto.DESC);

        long amount = userService.selectAmount(req);
        List<UserEntity> userEntityList = new ArrayList<>();
        if(amount>0){
            req.setTotalCount(amount);
            userEntityList = userService.selectListByPage(req);
        }
        PageResultDto<UserEntity> flowPage = new PageResultDto<UserEntity>(amount, req.getCurrentPage(), req.getPageSize(), userEntityList);
        return DubboResult.buildSuccessResult(flowPage);
    }

    @ApiOperation(value = "新增用户",notes = "")
    @ApiImplicitParam(name="user",value = "用户详细实体user",required = true,dataType ="userEntity")
    @RequestMapping(value = "save",method = RequestMethod.POST)
    public DubboResult<String> save(@Valid @RequestBody ReqUserDto req){
        String loginName =req.getLoginName();
        BizPreconditions.checkArgument(loginName != null, "登陆名不能为空");
        UserEntity user = new UserEntity().setLoginName(req.getLoginName()).
                setPassWord(req.getPassWord()).setCUserId("1").setMUserId("1");
        userService.insert(user);
        return DubboResult.buildSuccessResult(user.getUserId());
    }

    @ApiOperation(value = "修改用户",notes = "")
    @ApiImplicitParam(name="user",value = "用户详细实体user",required = true,dataType ="userEntity")
    @RequestMapping(value = "edit",method = RequestMethod.POST)
    public DubboResult edit(@Valid @RequestBody ReqUserDto req){
        Long userId = req.getUserId();
        BizPreconditions.checkNotNull(userService.selectById(userId),"用户不存在");
        String loginName =req.getLoginName();
        BizPreconditions.checkArgument(loginName != null, "登陆名不能为空");
        UserEntity user = new UserEntity();
        user.setUserName(req.getUserName());
        user.setPhone(req.getPhone());
        userService.update(user,new EntityWrapper<UserEntity>().eq("user_id",userId));
        return DubboResult.buildSuccessResult();

    }


    @ApiOperation(value = "删除用户",notes = "")
    @ApiImplicitParam(name="id",value = "用户id",required = true,dataType ="Long")
    @RequestMapping(value = "del",method = RequestMethod.POST)

    public DubboResult delete(@Valid @RequestBody ReqUserDto req){
        Long userId = req.getUserId();
        BizPreconditions.checkNotNull(userService.selectById(userId),"用户不存在");
        userService.deleteById(userId);
        return DubboResult.buildSuccessResult();

    }




}
